home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Util / Var / dll.c next >
Encoding:
C/C++ Source or Header  |  1994-12-22  |  4.8 KB  |  195 lines

  1.  
  2.  
  3. #include <lists.h>
  4. #include <clib/alib_protos.h>
  5. #include <clib/exec_protos.h>
  6. extern APTR SysBase;
  7. #include <pragmas/exec_pragmas.h>
  8.  
  9. #undef    Prototype
  10. #define Prototype extern
  11.  
  12.  
  13. #if 0
  14.  
  15. Prototype struct Node *DLL_GetHead (struct List *l);
  16. struct Node *DLL_GetHead (struct List *l) {
  17.     return l->lh_Head->ln_Succ ? l->lh_Head : NULL;
  18. } /* DLL_GetHead */
  19.  
  20. Prototype struct Node *DLL_GetTail (struct List *l);
  21. struct Node *DLL_GetTail (struct List *l) {
  22.     return l->lh_TailPred->ln_Succ ? l->lh_TailPred : NULL;
  23. } /* DLL_GetTail */
  24.  
  25. Prototype struct Node *DLL_GetSucc (struct List *l, struct Node *n);
  26. struct Node *DLL_GetSucc (struct List *l, struct Node *n) {
  27.     return n->ln_Succ->ln_Succ ? n->ln_Succ : NULL;
  28. } /* DLL_GetSucc */
  29.  
  30. struct Node *DLL_GetPred (struct List *l, struct Node *n) {
  31.     return (n->ln_Pred->ln_Pred ? n->ln_Pred : NULL;
  32. } /* DLL_GetPred */
  33.  
  34.  
  35. #endif
  36.  
  37. #define DLL_RemoveNode(l,n) Remove(n)
  38. #define DLL_GetHead(l)   GetHead(l)
  39. #define DLL_RemHead(l)   RemHead(l)
  40. #define DLL_RemTail(l)   RemTail(l)
  41. #define DLL_GetTail(l)   GetTail(l)
  42. #define DLL_GetSucc(l,n) GetSucc(n)
  43. #define DLL_GetPred(l,n) GetPred(n)
  44. #define DLL_AddHead(l,n) AddHead(l,n)
  45. #define DLL_AddTail(l,n) AddTail(l,n)
  46.  
  47.  
  48. #if 0
  49.  
  50.  
  51. Prototype void DLL_Sort (struct List *l, int (*comp)(struct Node *, struct Node *));
  52. void DLL_Sort (struct List *l, int (*comp)(struct Node *, struct Node *)) {
  53.     struct Node *n, *c;
  54.     struct List inter;
  55.     int diff;
  56.  
  57.     NewList (&inter);
  58.  
  59.     while (n = DLL_GetHead(l)) {
  60.     for (c = DLL_GetSucc(l,n); c; c = DLL_GetSucc(c)) {
  61.         diff = (*comp)(c, n);
  62.         if (diff < 0)
  63.         n = c;
  64.     } /* for */
  65.     DLL_RemoveNode (l, n);
  66.     DLL_AddHead(&inter, n);
  67.     } /* while */
  68.  
  69.     while (n = DLL_RemHead(&inter)) {
  70.     AddHead(l, n);
  71.     } /* while */
  72. } /* DLL_Sort */
  73.  
  74.  
  75. Prototype void DLL_Filter (struct List *l, struct List *dest, int (*check)(struct Node *, void *), void *ud);
  76. void DLL_Filter (struct List *l, struct List *dest, int (*check)(struct Node *, void *), void *ud) {
  77.     struct Node *n, *nn;
  78.     int chck;
  79.  
  80.     for (n = DLL_GetHead(l); n; n = nn) {
  81.     nn = DLL_GetSucc(l,n);
  82.  
  83.     chck = (*check)(n, ud);
  84.     if (chck) {
  85.         DLL_RemoveNode(l, n);
  86.         DLL_AddTail(dest, n);
  87.     } /* if */
  88.     } /* for */
  89. } /* DLL_Filter */
  90.  
  91.  
  92. Prototype void DLL_Scan (struct List *l, void (*scan)(struct Node *, void *, int), void *ud);
  93. void DLL_Scan (struct List *l, void (*scan)(struct Node *, void *, int), void *ud) {
  94.     struct Node *n, *nn;
  95.     int i = 0;
  96.     for (n = DLL_GetHead(l); n; n = nn) {
  97.     nn = DLL_GetSucc(l,n);
  98.     (*scan)(n, ud, i++);
  99.     } /* for */
  100. } /* DLL_Scan */
  101.  
  102.  
  103.  
  104. Prototype int DLL_Length (struct List *list);
  105. int DLL_Length (struct List *list) {
  106.     struct Node *nod;
  107.     int     num = 0;
  108.  
  109.     for (nod = DLL_GetHead (list); nod; nod = DLL_GetSucc (nod)) {
  110.     num ++;
  111.     } /* for */
  112.     return (num);
  113. } /* DLL_Length */
  114.  
  115.  
  116. #endif
  117.  
  118. Prototype struct Node *DLL_Search (struct List *l, int (*search)(struct Node *, void *), void *ud);
  119. struct Node *DLL_Search (struct List *l, int (*search)(struct Node *, void *), void *ud) {
  120.     struct Node *n;
  121.     for (n = DLL_GetHead(l); n; n = DLL_GetSucc(l,n)) {
  122.     if ((*search)(n, ud))
  123.         return n;
  124.     } /* for */
  125. } /* DLL_Search */
  126.  
  127.  
  128. Prototype struct Node *DLL_NumToNode (struct List *l, int num);
  129. struct Node *DLL_NumToNode (struct List *l, int num) {
  130.     struct Node *n;
  131.     if (num >= 0) {
  132.     for (n = DLL_GetHead (l); (num > 0) && (n != NULL); n = DLL_GetSucc (l,n), num --);
  133.     } else {
  134.     for (n = DLL_GetTail (l); (num < 0) && (n != NULL); n = DLL_GetPred (l,n), num ++);
  135.     } /* if */
  136.  
  137.     return n;
  138. } /* DLL_NumToNode */
  139.  
  140.  
  141. Prototype int DLL_NodeToNum (struct List *l, struct Node *n);
  142. int DLL_NodeToNum (struct List *l, struct Node *n) {
  143.     int num = 0;
  144.  
  145.     if (n != NULL) {
  146.     while (n = DLL_GetPred (l,n)) {
  147.         num ++;
  148.     } /* while */
  149.  
  150.     return (num);
  151.     } /* if */
  152.     return (-1);
  153. } /* DLL_NodeToNum */
  154.  
  155.  
  156. Prototype void DLL_Join (struct List *dest, struct List *add);
  157. void DLL_Join (struct List *dest, struct List *add) {
  158. #if 1
  159.     struct Node *inter;
  160.  
  161.     while (inter = DLL_RemHead (add)) {
  162.     AddTail (dest, inter);
  163.     } /* while */
  164. #else
  165.     struct Node *last    = dest->lh_TailPred;
  166.     struct Node *afirst = DLL_GetHead (add);
  167.  
  168.     if (afirst) {
  169.     last->ln_Succ    = afirst;
  170.     afirst->ln_Pred = last;
  171.     last = DLL_GetTail (add);
  172.  
  173.     last->ln_Succ      = &dest->lh_Tail;
  174.     dest->lh_TailPred = last;
  175.  
  176.     NewList (add);
  177.     } /* if */
  178. #endif /* NOT_DEF */
  179. } /* DLL_Join */
  180.  
  181.  
  182. Prototype void DLL_AddSorted (struct List *l, struct Node *n, int (*comp)(struct Node *, struct Node *, void *), void *ud);
  183. void DLL_AddSorted (struct List *l, struct Node *n, int (*comp)(struct Node *, struct Node *, void *), void *ud) {
  184.     struct Node *m;
  185.     for (m = DLL_GetHead(l); m && ((*comp)(m, n, ud) < 0); m = DLL_GetSucc (l,m));
  186.     if (!m) {
  187.     DLL_AddTail(l,n);
  188.     } else {
  189.     Insert(l, n, m->ln_Pred);
  190.     }
  191. } /* DLL_AddSorted */
  192.  
  193.  
  194.  
  195.